/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusServiceRel;
import han.bifang.gwt.client.common.entity.Role;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class ServiceDetailContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private VerticalPanel panel = null;

	private ServiceMessages cons;

	private Service ser;

	private ArrayList ds = null;
	private String id = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			ser = (Service) ds.get(0);
			ArrayList qos = (ArrayList) ds.get(1);
			BusServiceRel r = (BusServiceRel) ds.get(2);
			ArrayList vl = (ArrayList) ds.get(3);
			String resourceId = (String) ds.get(4);
			ArrayList<Role> rl = (ArrayList<Role>) ds.get(5);

			TabPanel tab = new TabPanel();
			tab.add(new ServiceBasicContent(cons, service, context, ser, vl),
					cons.basic());
			tab.add(new ServiceConfigContent(cons, service, context, qos), cons
					.config());
			tab.add(new ServiceBusBindingContent(cons, service, context, r),
					cons.bus());
			
			tab.add(new ServiceRoleRefContent(cons, service, context,
					resourceId, rl), cons.security());
			
			tab.add(new ServiceStatContent(cons,service,context,ser.getServiceCode()),cons.statistic());

			tab.selectTab(0);
			tab.setWidth("100%");
			panel.add(tab);
			panel.add(new HTML("&nbsp;"));

			HorizontalPanel h1 = new HorizontalPanel();

			Button b = new Button(cons.addVersion());
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					context.put(ServiceConstants.SERVICE, ser);

					listener.onContentChanged(
							ServiceContentConstants.SERVICE_ROOT,
							ServiceContentConstants.CREATE_VERSION_BASIC, null);
				}

			});
			if (PrivilegChecker.hasPrivilege(context, null)) {
				h1.add(b);
				h1.add(new HTML("&nbsp;&nbsp;"));
			}
			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());
					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.REMOVE_SERVICE, null);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				h1.add(b1);
			h1.add(new HTML("&nbsp;&nbsp;"));

			Button b2 = new Button(cons.ret());
			b2.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());

					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.SEARCH_RESULT, null);

				}

			});
			h1.add(b2);

			panel.add(h1);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public ServiceDetailContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext ctx, ArrayList ds,
			String id) {
		this.cons = cons;
		this.service = service;
		this.context = ctx;
		this.ds = ds;
		this.id = id;
		panel = new VerticalPanel();
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		if (ds != null) {
			ser = (Service) ds.get(0);
			ArrayList qos = (ArrayList) ds.get(1);
			BusServiceRel r = (BusServiceRel) ds.get(2);
			ArrayList vl = (ArrayList) ds.get(3);
			String resourceId = (String) ds.get(4);
			ArrayList<Role> rl = (ArrayList<Role>) ds.get(5);

			context.put(ServiceConstants.SERVICE, ser);
			TabPanel tab = new TabPanel();
			tab.add(new ServiceBasicContent(cons, service, context, ser, vl),
					cons.basic());
			tab.add(new ServiceConfigContent(cons, service, context, qos), cons
					.config());
			tab.add(new ServiceBusBindingContent(cons, service, context, r),
					cons.bus());
			tab.add(new ServiceRoleRefContent(cons, service, context,
					resourceId, rl), cons.security());
			
			tab.add(new ServiceStatContent(cons,service,context,ser.getServiceCode()),cons.statistic());

			tab.selectTab(0);
			tab.setWidth("100%");
			panel.add(tab);
			panel.add(new HTML("&nbsp;"));

			HorizontalPanel h1 = new HorizontalPanel();

			Button b = new Button(cons.addVersion());
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {

					listener.onContentChanged(
							ServiceContentConstants.SERVICE_ROOT,
							ServiceContentConstants.CREATE_VERSION_BASIC, null);
				}

			});
			if (PrivilegChecker.hasPrivilege(context, null)) {
				h1.add(b);
				h1.add(new HTML("&nbsp;&nbsp;"));
			}
			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());
					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.REMOVE_SERVICE, null);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				h1.add(b1);
			h1.add(new HTML("&nbsp;&nbsp;"));

			Button b2 = new Button(cons.ret());
			b2.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());

					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.SEARCH_RESULT, null);

				}

			});
			h1.add(b2);

			panel.add(h1);
		} else {
			RequestObject req = new RequestObject();
			req.setRequest(id);
			req
					.setAction("han.bifang.gwt.server.action.service.GetServiceDetailAction");
			service.call(req, cb);
		}
	}

}
